from django.http import HttpResponse, JsonResponse
from django.utils.decorators import method_decorator
from django.views import View
from django.views.decorators.csrf import csrf_exempt

from utils.Robot.client import create_menu
from utils.Robot.config import Access_Token

# Create your views here.


@method_decorator(csrf_exempt, name='dispatch')

class TestAPI(View):

    def get(self,request):
        #接收微信服务器get请求发过来的参数
        signature = request.GET.get('signature', None)
        timestamp = request.GET.get('timestamp', None)
        nonce = request.GET.get('nonce', None)
        echostr = request.GET.get('echostr', None)
        #服务器配置中的token
        token = Access_Token
        #把参数放到list中排序后合成一个字符串，再用sha1加密得到新的字符串与微信发来的signature对比，如果相同就返回echostr给服务器，校验通过
        return HttpResponse(echostr)



# TODO 微信公众号通知
class SendWeChatAPI(View):

    def get(self, request):
        pass

    def post(self, request):
        pass



# TODO 公众号菜单更新
class UpdateMenu(View):

    def get(self, request):
        return HttpResponse(create_menu())

    def post(self, request):
        pass